merged master
[lhc/web/wiklou.git] / includes / filerepo / FileRepo.php
index 6077722..d4eef87 100644 (file)
 <?php
+/**
+ * @defgroup FileRepo File Repository
+ *
+ * @brief This module handles how MediaWiki interacts with filesystems.
+ *
+ * @details
+ */
+
 /**
  * Base code for file repositories.
  *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
  * @file
  * @ingroup FileRepo
  */
 
 /**
- * Base class for file repositories.
- * Do not instantiate, use a derived class.
+ * Base class for file repositories
  *
  * @ingroup FileRepo
  */
-abstract class FileRepo {
-       const FILES_ONLY = 1;
+class FileRepo {
        const DELETE_SOURCE = 1;
        const OVERWRITE = 2;
        const OVERWRITE_SAME = 4;
-       const SKIP_VALIDATION = 8;
+       const SKIP_LOCKING = 8;
+
+       /** @var FileBackend */
+       protected $backend;
+       /** @var Array Map of zones to config */
+       protected $zones = array();
 
        var $thumbScriptUrl, $transformVia404;
        var $descBaseUrl, $scriptDirUrl, $scriptExtension, $articleUrl;
        var $fetchDescription, $initialCapital;
-       var $pathDisclosureProtection = 'paranoid';
-       var $descriptionCacheExpiry, $hashLevels, $url, $thumbUrl;
+       var $pathDisclosureProtection = 'simple'; // 'paranoid'
+       var $descriptionCacheExpiry, $url, $thumbUrl;
+       var $hashLevels, $deletedHashLevels;
 
        /**
         * Factory functions for creating new files
         * Override these in the base class
         */
-       var $fileFactory = false, $oldFileFactory = false;
+       var $fileFactory = array( 'UnregisteredLocalFile', 'newFromTitle' );
+       var $oldFileFactory = false;
        var $fileFactoryKey = false, $oldFileFactoryKey = false;
 
-       function __construct( $info ) {
+       /**
+        * @param $info array|null
+        * @throws MWException
+        */
+       public function __construct( array $info = null ) {
+               // Verify required settings presence
+               if(
+                       $info === null
+                       || !array_key_exists( 'name', $info )
+                       || !array_key_exists( 'backend', $info )
+               ) {
+                       throw new MWException( __CLASS__ . " requires an array of options having both 'name' and 'backend' keys.\n" );
+               }
+
                // Required settings
                $this->name = $info['name'];
+               if ( $info['backend'] instanceof FileBackend ) {
+                       $this->backend = $info['backend']; // useful for testing
+               } else {
+                       $this->backend = FileBackendGroup::singleton()->get( $info['backend'] );
+               }
 
-               // Optional settings
-               $this->initialCapital = MWNamespace::isCapitalized( NS_FILE );
-               foreach ( array( 'descBaseUrl', 'scriptDirUrl', 'articleUrl', 'fetchDescription',
-                       'thumbScriptUrl', 'initialCapital', 'pathDisclosureProtection',
-                       'descriptionCacheExpiry', 'hashLevels', 'url', 'thumbUrl', 'scriptExtension' ) 
-                       as $var )
-               {
+               // Optional settings that can have no value
+               $optionalSettings = array(
+                       'descBaseUrl', 'scriptDirUrl', 'articleUrl', 'fetchDescription',
+                       'thumbScriptUrl', 'pathDisclosureProtection', 'descriptionCacheExpiry',
+                       'scriptExtension'
+               );
+               foreach ( $optionalSettings as $var ) {
                        if ( isset( $info[$var] ) ) {
                                $this->$var = $info[$var];
                        }
                }
+
+               // Optional settings that have a default
+               $this->initialCapital = isset( $info['initialCapital'] )
+                       ? $info['initialCapital']
+                       : MWNamespace::isCapitalized( NS_FILE );
+               $this->url = isset( $info['url'] )
+                       ? $info['url']
+                       : false; // a subclass may set the URL (e.g. ForeignAPIRepo)
+               if ( isset( $info['thumbUrl'] ) ) {
+                       $this->thumbUrl = $info['thumbUrl'];
+               } else {
+                       $this->thumbUrl = $this->url ? "{$this->url}/thumb" : false;
+               }
+               $this->hashLevels = isset( $info['hashLevels'] )
+                       ? $info['hashLevels']
+                       : 2;
+               $this->deletedHashLevels = isset( $info['deletedHashLevels'] )
+                       ? $info['deletedHashLevels']
+                       : $this->hashLevels;
                $this->transformVia404 = !empty( $info['transformVia404'] );
+               $this->zones = isset( $info['zones'] )
+                       ? $info['zones']
+                       : array();
+               // Give defaults for the basic zones...
+               foreach ( array( 'public', 'thumb', 'temp', 'deleted' ) as $zone ) {
+                       if ( !isset( $this->zones[$zone]['container'] ) ) {
+                               $this->zones[$zone]['container'] = "{$this->name}-{$zone}";
+                       }
+                       if ( !isset( $this->zones[$zone]['directory'] ) ) {
+                               $this->zones[$zone]['directory'] = '';
+                       }
+               }
+       }
+
+       /**
+        * Get the file backend instance. Use this function wisely.
+        *
+        * @return FileBackend
+        */
+       public function getBackend() {
+               return $this->backend;
+       }
+
+       /**
+        * Get an explanatory message if this repo is read-only.
+        * This checks if an administrator disabled writes to the backend.
+        *
+        * @return string|bool Returns false if the repo is not read-only
+        */
+       public function getReadOnlyReason() {
+               return $this->backend->getReadOnlyReason();
+       }
+
+       /**
+        * Check if a single zone or list of zones is defined for usage
+        *
+        * @param $doZones Array Only do a particular zones
+        * @throws MWException
+        * @return Status
+        */
+       protected function initZones( $doZones = array() ) {
+               $status = $this->newGood();
+               foreach ( (array)$doZones as $zone ) {
+                       $root = $this->getZonePath( $zone );
+                       if ( $root === null ) {
+                               throw new MWException( "No '$zone' zone defined in the {$this->name} repo." );
+                       }
+               }
+               return $status;
+       }
+
+       /**
+        * Take all available measures to prevent web accessibility of new deleted
+        * directories, in case the user has not configured offline storage
+        *
+        * @param $dir string
+        * @return void
+        */
+       protected function initDeletedDir( $dir ) {
+               $this->backend->secure( // prevent web access & dir listings
+                       array( 'dir' => $dir, 'noAccess' => true, 'noListing' => true ) );
        }
 
        /**
         * Determine if a string is an mwrepo:// URL
+        *
+        * @param $url string
+        * @return bool
         */
-       static function isVirtualUrl( $url ) {
+       public static function isVirtualUrl( $url ) {
                return substr( $url, 0, 9 ) == 'mwrepo://';
        }
 
+       /**
+        * Get a URL referring to this repository, with the private mwrepo protocol.
+        * The suffix, if supplied, is considered to be unencoded, and will be
+        * URL-encoded before being returned.
+        *
+        * @param $suffix string|bool
+        * @return string
+        */
+       public function getVirtualUrl( $suffix = false ) {
+               $path = 'mwrepo://' . $this->name;
+               if ( $suffix !== false ) {
+                       $path .= '/' . rawurlencode( $suffix );
+               }
+               return $path;
+       }
+
+       /**
+        * Get the URL corresponding to one of the four basic zones
+        *
+        * @param $zone String: one of: public, deleted, temp, thumb
+        * @return String or false
+        */
+       public function getZoneUrl( $zone ) {
+               if ( isset( $this->zones[$zone]['url'] )
+                       && in_array( $zone, array( 'public', 'temp', 'thumb' ) ) )
+               {
+                       return $this->zones[$zone]['url']; // custom URL
+               }
+               switch ( $zone ) {
+                       case 'public':
+                               return $this->url;
+                       case 'temp':
+                               return "{$this->url}/temp";
+                       case 'deleted':
+                               return false; // no public URL
+                       case 'thumb':
+                               return $this->thumbUrl;
+                       default:
+                               return false;
+               }
+       }
+
+       /**
+        * Get the backend storage path corresponding to a virtual URL.
+        * Use this function wisely.
+        *
+        * @param $url string
+        * @throws MWException
+        * @return string
+        */
+       public function resolveVirtualUrl( $url ) {
+               if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
+                       throw new MWException( __METHOD__.': unknown protocol' );
+               }
+               $bits = explode( '/', substr( $url, 9 ), 3 );
+               if ( count( $bits ) != 3 ) {
+                       throw new MWException( __METHOD__.": invalid mwrepo URL: $url" );
+               }
+               list( $repo, $zone, $rel ) = $bits;
+               if ( $repo !== $this->name ) {
+                       throw new MWException( __METHOD__.": fetching from a foreign repo is not supported" );
+               }
+               $base = $this->getZonePath( $zone );
+               if ( !$base ) {
+                       throw new MWException( __METHOD__.": invalid zone: $zone" );
+               }
+               return $base . '/' . rawurldecode( $rel );
+       }
+
+       /**
+        * The the storage container and base path of a zone
+        *
+        * @param $zone string
+        * @return Array (container, base path) or (null, null)
+        */
+       protected function getZoneLocation( $zone ) {
+               if ( !isset( $this->zones[$zone] ) ) {
+                       return array( null, null ); // bogus
+               }
+               return array( $this->zones[$zone]['container'], $this->zones[$zone]['directory'] );
+       }
+
+       /**
+        * Get the storage path corresponding to one of the zones
+        *
+        * @param $zone string
+        * @return string|null Returns null if the zone is not defined
+        */
+       public function getZonePath( $zone ) {
+               list( $container, $base ) = $this->getZoneLocation( $zone );
+               if ( $container === null || $base === null ) {
+                       return null;
+               }
+               $backendName = $this->backend->getName();
+               if ( $base != '' ) { // may not be set
+                       $base = "/{$base}";
+               }
+               return "mwstore://$backendName/{$container}{$base}";
+       }
+
        /**
         * Create a new File object from the local repository
         *
@@ -66,15 +295,12 @@ abstract class FileRepo {
         *              instance of the repository's old file class instead of a
         *              current file. Repositories not supporting version control
         *              should return false if this parameter is set.
-        *
-        * @return File
+        * @return File|null A File, or null if passed an invalid Title
         */
-       function newFile( $title, $time = false ) {
-               if ( !($title instanceof Title) ) {
-                       $title = Title::makeTitleSafe( NS_FILE, $title );
-                       if ( !is_object( $title ) ) {
-                               return null;
-                       }
+       public function newFile( $title, $time = false ) {
+               $title = File::normalizeTitle( $title );
+               if ( !$title ) {
+                       return null;
                }
                if ( $time ) {
                        if ( $this->oldFileFactory ) {
@@ -93,30 +319,24 @@ abstract class FileRepo {
         * version control should return false if the time is specified.
         *
         * @param $title Mixed: Title object or string
-        * @param $options Associative array of options:
-        *     time:           requested time for an archived image, or false for the
+        * @param $options array Associative array of options:
+        *     time:           requested time for a specific file version, or false for the
         *                     current version. An image object will be returned which was
-        *                     created at the specified time.
+        *                     created at the specified time (which may be archived or current).
         *
         *     ignoreRedirect: If true, do not follow file redirects
         *
         *     private:        If true, return restricted (deleted) files if the current
         *                     user is allowed to view them. Otherwise, such files will not
         *                     be found.
+        * @return File|bool False on failure
         */
-       function findFile( $title, $options = array() ) {
-               if ( !is_array( $options ) ) {
-                       // MW 1.15 compat
-                       $time = $options;
-               } else {
-                       $time = isset( $options['time'] ) ? $options['time'] : false;
-               }
-               if ( !($title instanceof Title) ) {
-                       $title = Title::makeTitleSafe( NS_FILE, $title );
-                       if ( !is_object( $title ) ) {
-                               return false;
-                       }
+       public function findFile( $title, $options = array() ) {
+               $title = File::normalizeTitle( $title );
+               if ( !$title ) {
+                       return false;
                }
+               $time = isset( $options['time'] ) ? $options['time'] : false;
                # First try the current version of the file to see if it precedes the timestamp
                $img = $this->newFile( $title );
                if ( !$img ) {
@@ -129,9 +349,9 @@ abstract class FileRepo {
                if ( $time !== false ) {
                        $img = $this->newFile( $title, $time );
                        if ( $img && $img->exists() ) {
-                               if ( !$img->isDeleted(File::DELETED_FILE) ) {
-                                       return $img;
-                               } else if ( !empty( $options['private'] )  && $img->userCan(File::DELETED_FILE) ) {
+                               if ( !$img->isDeleted( File::DELETED_FILE ) ) {
+                                       return $img; // always OK
+                               } elseif ( !empty( $options['private'] ) && $img->userCan( File::DELETED_FILE ) ) {
                                        return $img;
                                }
                        }
@@ -142,12 +362,12 @@ abstract class FileRepo {
                        return false;
                }
                $redir = $this->checkRedirect( $title );
-               if( $redir && $title->getNamespace() == NS_FILE) {
+               if ( $redir && $title->getNamespace() == NS_FILE) {
                        $img = $this->newFile( $redir );
-                       if( !$img ) {
+                       if ( !$img ) {
                                return false;
                        }
-                       if( $img->exists() ) {
+                       if ( $img->exists() ) {
                                $img->redirectedFrom( $title->getDBkey() );
                                return $img;
                        }
@@ -155,16 +375,18 @@ abstract class FileRepo {
                return false;
        }
 
-       /*
+       /**
         * Find many files at once.
-        * @param $items An array of titles, or an array of findFile() options with
+        *
+        * @param $items array An array of titles, or an array of findFile() options with
         *    the "title" option giving the title. Example:
         *
         *     $findItem = array( 'title' => $title, 'private' => true );
         *     $findBatch = array( $findItem );
         *     $repo->findFiles( $findBatch );
+        * @return array
         */
-       function findFiles( $items ) {
+       public function findFiles( array $items ) {
                $result = array();
                foreach ( $items as $item ) {
                        if ( is_array( $item ) ) {
@@ -183,60 +405,33 @@ abstract class FileRepo {
                return $result;
        }
 
-       /**
-        * Create a new File object from the local repository
-        * @param $sha1 Mixed: SHA-1 key
-        * @param $time Mixed: time at which the image was uploaded.
-        *              If this is specified, the returned object will be an
-        *              of the repository's old file class instead of a current
-        *              file. Repositories not supporting version control should
-        *              return false if this parameter is set.
-        *
-        * @return File
-        */
-       function newFileFromKey( $sha1, $time = false ) {
-               if ( $time ) {
-                       if ( $this->oldFileFactoryKey ) {
-                               return call_user_func( $this->oldFileFactoryKey, $sha1, $this, $time );
-                       } else {
-                               return false;
-                       }
-               } else {
-                       return call_user_func( $this->fileFactoryKey, $sha1, $this );
-               }
-       }
-
        /**
         * Find an instance of the file with this key, created at the specified time
         * Returns false if the file does not exist. Repositories not supporting
         * version control should return false if the time is specified.
         *
-        * @param $sha1 String
-        * @param $options Option array, same as findFile().
+        * @param $sha1 String base 36 SHA-1 hash
+        * @param $options array Option array, same as findFile().
+        * @return File|bool False on failure
         */
-       function findFileFromKey( $sha1, $options = array() ) {
-               if ( !is_array( $options ) ) {
-                       # MW 1.15 compat
-                       $time = $options;
+       public function findFileFromKey( $sha1, $options = array() ) {
+               $time = isset( $options['time'] ) ? $options['time'] : false;
+               # First try to find a matching current version of a file...
+               if ( $this->fileFactoryKey ) {
+                       $img = call_user_func( $this->fileFactoryKey, $sha1, $this, $time );
                } else {
-                       $time = isset( $options['time'] ) ? $options['time'] : false;
+                       return false; // find-by-sha1 not supported
                }
-
-               # First try the current version of the file to see if it precedes the timestamp
-               $img = $this->newFileFromKey( $sha1 );
-               if ( !$img ) {
-                       return false;
-               }
-               if ( $img->exists() && ( !$time || $img->getTimestamp() == $time ) ) {
+               if ( $img && $img->exists() ) {
                        return $img;
                }
-               # Now try an old version of the file
-               if ( $time !== false ) {
-                       $img = $this->newFileFromKey( $sha1, $time );
-                       if ( $img->exists() ) {
-                               if ( !$img->isDeleted(File::DELETED_FILE) ) {
-                                       return $img;
-                               } else if ( !empty( $options['private'] ) && $img->userCan(File::DELETED_FILE) ) {
+               # Now try to find a matching old version of a file...
+               if ( $time !== false && $this->oldFileFactoryKey ) { // find-by-sha1 supported?
+                       $img = call_user_func( $this->oldFileFactoryKey, $sha1, $this, $time );
+                       if ( $img && $img->exists() ) {
+                               if ( !$img->isDeleted( File::DELETED_FILE ) ) {
+                                       return $img; // always OK
+                               } elseif ( !empty( $options['private'] ) && $img->userCan( File::DELETED_FILE ) ) {
                                        return $img;
                                }
                        }
@@ -245,35 +440,54 @@ abstract class FileRepo {
        }
 
        /**
-        * Get the URL of thumb.php
+        * Get an array or iterator of file objects for files that have a given
+        * SHA-1 content hash.
+        *
+        * STUB
+        * @param $hash
+        * @return array
         */
-       function getThumbScriptUrl() {
-               return $this->thumbScriptUrl;
+       public function findBySha1( $hash ) {
+               return array();
        }
 
        /**
-        * Get the URL corresponding to one of the four basic zones
-        * @param $zone String: one of: public, deleted, temp, thumb
-        * @return String or false
+        * Get the public root URL of the repository
+        *
+        * @deprecated since 1.20
+        * @return string
         */
-       function getZoneUrl( $zone ) {
-               return false;
+       public function getRootUrl() {
+               return $this->getZoneUrl( 'public' );
+       }
+
+       /**
+        * Get the URL of thumb.php
+        *
+        * @return string
+        */
+       public function getThumbScriptUrl() {
+               return $this->thumbScriptUrl;
        }
 
        /**
         * Returns true if the repository can transform files via a 404 handler
+        *
+        * @return bool
         */
-       function canTransformVia404() {
+       public function canTransformVia404() {
                return $this->transformVia404;
        }
 
        /**
         * Get the name of an image from its title object
+        *
         * @param $title Title
+        * @return String
         */
-       function getNameFromTitle( $title ) {
+       public function getNameFromTitle( Title $title ) {
+               global $wgContLang;
                if ( $this->initialCapital != MWNamespace::isCapitalized( NS_FILE ) ) {
-                       global $wgContLang;
                        $name = $title->getUserCaseDBKey();
                        if ( $this->initialCapital ) {
                                $name = $wgContLang->ucfirst( $name );
@@ -284,7 +498,45 @@ abstract class FileRepo {
                return $name;
        }
 
-       static function getHashPathForLevel( $name, $levels ) {
+       /**
+        * Get the public zone root storage directory of the repository
+        *
+        * @return string
+        */
+       public function getRootDirectory() {
+               return $this->getZonePath( 'public' );
+       }
+
+       /**
+        * Get a relative path including trailing slash, e.g. f/fa/
+        * If the repo is not hashed, returns an empty string
+        *
+        * @param $name string Name of file
+        * @return string
+        */
+       public function getHashPath( $name ) {
+               return self::getHashPathForLevel( $name, $this->hashLevels );
+       }
+
+       /**
+        * Get a relative path including trailing slash, e.g. f/fa/
+        * If the repo is not hashed, returns an empty string
+        *
+        * @param $suffix string Basename of file from FileRepo::storeTemp()
+        * @return string
+        */
+       public function getTempHashPath( $suffix ) {
+               $parts = explode( '!', $suffix, 2 ); // format is <timestamp>!<name> or just <name>
+               $name = isset( $parts[1] ) ? $parts[1] : $suffix; // hash path is not based on timestamp
+               return self::getHashPathForLevel( $name, $this->hashLevels );
+       }
+
+       /**
+        * @param $name
+        * @param $levels
+        * @return string
+        */
+       protected static function getHashPathForLevel( $name, $levels ) {
                if ( $levels == 0 ) {
                        return '';
                } else {
@@ -298,31 +550,37 @@ abstract class FileRepo {
        }
 
        /**
-        * Get a relative path including trailing slash, e.g. f/fa/
-        * If the repo is not hashed, returns an empty string
+        * Get the number of hash directory levels
+        *
+        * @return integer
         */
-       function getHashPath( $name ) {
-               return self::getHashPathForLevel( $name, $this->hashLevels );
+       public function getHashLevels() {
+               return $this->hashLevels;
        }
 
        /**
         * Get the name of this repository, as specified by $info['name]' to the constructor
+        *
+        * @return string
         */
-       function getName() {
+       public function getName() {
                return $this->name;
        }
-       
+
        /**
         * Make an url to this repo
-        * 
+        *
         * @param $query mixed Query string to append
         * @param $entry string Entry point; defaults to index
-        * @return string
+        * @return string|bool False on failure
         */
-       function makeUrl( $query = '', $entry = 'index' ) {
-               $ext = isset( $this->scriptExtension ) ? $this->scriptExtension : '.php';
-               return wfAppendQuery( "{$this->scriptDirUrl}/{$entry}{$ext}", $query ); 
-       } 
+       public function makeUrl( $query = '', $entry = 'index' ) {
+               if ( isset( $this->scriptDirUrl ) ) {
+                       $ext = isset( $this->scriptExtension ) ? $this->scriptExtension : '.php';
+                       return wfAppendQuery( "{$this->scriptDirUrl}/{$entry}{$ext}", $query );
+               }
+               return false;
+       }
 
        /**
         * Get the URL of an image description page. May return false if it is
@@ -332,8 +590,11 @@ abstract class FileRepo {
         *
         * In particular, it uses the article paths as specified to the repository
         * constructor, whereas local repositories use the local Title functions.
+        *
+        * @param $name string
+        * @return string
         */
-       function getDescriptionUrl( $name ) {
+       public function getDescriptionUrl( $name ) {
                $encName = wfUrlencode( $name );
                if ( !is_null( $this->descBaseUrl ) ) {
                        # "http://example.com/wiki/Image:"
@@ -363,16 +624,18 @@ abstract class FileRepo {
         * MediaWiki this means action=render. This should only be called by the
         * repository's file class, since it may return invalid results. User code
         * should use File::getDescriptionText().
+        *
         * @param $name String: name of image to fetch
         * @param $lang String: language to fetch it in, if any.
+        * @return string
         */
-       function getDescriptionRenderUrl( $name, $lang = null ) {
+       public function getDescriptionRenderUrl( $name, $lang = null ) {
                $query = 'action=render';
                if ( !is_null( $lang ) ) {
                        $query .= '&uselang=' . $lang;
                }
                if ( isset( $this->scriptDirUrl ) ) {
-                       return $this->makeUrl( 
+                       return $this->makeUrl(
                                'title=' .
                                wfUrlencode( 'Image:' . $name ) .
                                "&$query" );
@@ -385,22 +648,24 @@ abstract class FileRepo {
                        }
                }
        }
-       
+
        /**
         * Get the URL of the stylesheet to apply to description pages
-        * @return string
+        *
+        * @return string|bool False on failure
         */
-       function getDescriptionStylesheetUrl() {
-               if ( $this->scriptDirUrl ) {
+       public function getDescriptionStylesheetUrl() {
+               if ( isset( $this->scriptDirUrl ) ) {
                        return $this->makeUrl( 'title=MediaWiki:Filepage.css&' .
                                wfArrayToCGI( Skin::getDynamicStylesheetQuery() ) );
                }
+               return false;
        }
 
        /**
         * Store a file to a given destination.
         *
-        * @param $srcPath String: source path or virtual URL
+        * @param $srcPath String: source FS path, storage path, or virtual URL
         * @param $dstZone String: destination zone
         * @param $dstRel String: destination relative path
         * @param $flags Integer: bitwise combination of the following flags:
@@ -408,70 +673,336 @@ abstract class FileRepo {
         *     self::OVERWRITE         Overwrite an existing destination file instead of failing
         *     self::OVERWRITE_SAME    Overwrite the file if the destination exists and has the
         *                             same contents as the source
+        *     self::SKIP_LOCKING      Skip any file locking when doing the store
         * @return FileRepoStatus
         */
-       function store( $srcPath, $dstZone, $dstRel, $flags = 0 ) {
+       public function store( $srcPath, $dstZone, $dstRel, $flags = 0 ) {
+               $this->assertWritableRepo(); // fail out if read-only
+
                $status = $this->storeBatch( array( array( $srcPath, $dstZone, $dstRel ) ), $flags );
                if ( $status->successCount == 0 ) {
                        $status->ok = false;
                }
+
                return $status;
        }
 
        /**
         * Store a batch of files
         *
-        * @param $triplets Array: (src,zone,dest) triplets as per store()
-        * @param $flags Integer: flags as per store
+        * @param $triplets Array: (src, dest zone, dest rel) triplets as per store()
+        * @param $flags Integer: bitwise combination of the following flags:
+        *     self::DELETE_SOURCE     Delete the source file after upload
+        *     self::OVERWRITE         Overwrite an existing destination file instead of failing
+        *     self::OVERWRITE_SAME    Overwrite the file if the destination exists and has the
+        *                             same contents as the source
+        *     self::SKIP_LOCKING      Skip any file locking when doing the store
+        * @throws MWException
+        * @return FileRepoStatus
+        */
+       public function storeBatch( array $triplets, $flags = 0 ) {
+               $this->assertWritableRepo(); // fail out if read-only
+
+               $status = $this->newGood();
+               $backend = $this->backend; // convenience
+
+               $operations = array();
+               $sourceFSFilesToDelete = array(); // cleanup for disk source files
+               // Validate each triplet and get the store operation...
+               foreach ( $triplets as $triplet ) {
+                       list( $srcPath, $dstZone, $dstRel ) = $triplet;
+                       wfDebug( __METHOD__
+                               . "( \$src='$srcPath', \$dstZone='$dstZone', \$dstRel='$dstRel' )\n"
+                       );
+
+                       // Resolve destination path
+                       $root = $this->getZonePath( $dstZone );
+                       if ( !$root ) {
+                               throw new MWException( "Invalid zone: $dstZone" );
+                       }
+                       if ( !$this->validateFilename( $dstRel ) ) {
+                               throw new MWException( 'Validation error in $dstRel' );
+                       }
+                       $dstPath = "$root/$dstRel";
+                       $dstDir  = dirname( $dstPath );
+                       // Create destination directories for this triplet
+                       if ( !$backend->prepare( array( 'dir' => $dstDir ) )->isOK() ) {
+                               return $this->newFatal( 'directorycreateerror', $dstDir );
+                       }
+
+                       if ( $dstZone == 'deleted' ) {
+                               $this->initDeletedDir( $dstDir );
+                       }
+
+                       // Resolve source to a storage path if virtual
+                       $srcPath = $this->resolveToStoragePath( $srcPath );
+
+                       // Get the appropriate file operation
+                       if ( FileBackend::isStoragePath( $srcPath ) ) {
+                               $opName = ( $flags & self::DELETE_SOURCE ) ? 'move' : 'copy';
+                       } else {
+                               $opName = 'store';
+                               if ( $flags & self::DELETE_SOURCE ) {
+                                       $sourceFSFilesToDelete[] = $srcPath;
+                               }
+                       }
+                       $operations[] = array(
+                               'op'            => $opName,
+                               'src'           => $srcPath,
+                               'dst'           => $dstPath,
+                               'overwrite'     => $flags & self::OVERWRITE,
+                               'overwriteSame' => $flags & self::OVERWRITE_SAME,
+                       );
+               }
+
+               // Execute the store operation for each triplet
+               $opts = array( 'force' => true );
+               if ( $flags & self::SKIP_LOCKING ) {
+                       $opts['nonLocking'] = true;
+               }
+               $status->merge( $backend->doOperations( $operations, $opts ) );
+               // Cleanup for disk source files...
+               foreach ( $sourceFSFilesToDelete as $file ) {
+                       wfSuppressWarnings();
+                       unlink( $file ); // FS cleanup
+                       wfRestoreWarnings();
+               }
+
+               return $status;
+       }
+
+       /**
+        * Deletes a batch of files.
+        * Each file can be a (zone, rel) pair, virtual url, storage path.
+        * It will try to delete each file, but ignores any errors that may occur.
+        *
+        * @param $files array List of files to delete
+        * @param $flags Integer: bitwise combination of the following flags:
+        *     self::SKIP_LOCKING      Skip any file locking when doing the deletions
+        * @return FileRepoStatus
+        */
+       public function cleanupBatch( array $files, $flags = 0 ) {
+               $this->assertWritableRepo(); // fail out if read-only
+
+               $status = $this->newGood();
+
+               $operations = array();
+               foreach ( $files as $path ) {
+                       if ( is_array( $path ) ) {
+                               // This is a pair, extract it
+                               list( $zone, $rel ) = $path;
+                               $path = $this->getZonePath( $zone ) . "/$rel";
+                       } else {
+                               // Resolve source to a storage path if virtual
+                               $path = $this->resolveToStoragePath( $path );
+                       }
+                       $operations[] = array( 'op' => 'delete', 'src' => $path );
+               }
+               // Actually delete files from storage...
+               $opts = array( 'force' => true );
+               if ( $flags & self::SKIP_LOCKING ) {
+                       $opts['nonLocking'] = true;
+               }
+               $status->merge( $this->backend->doOperations( $operations, $opts ) );
+
+               return $status;
+       }
+
+       /**
+        * Import a file from the local file system into the repo.
+        * This does no locking nor journaling and overrides existing files.
+        * This function can be used to write to otherwise read-only foreign repos.
+        * This is intended for copying generated thumbnails into the repo.
+        *
+        * @param $src string File system path
+        * @param $dst string Virtual URL or storage path
+        * @return FileRepoStatus
+        */
+       final public function quickImport( $src, $dst ) {
+               return $this->quickImportBatch( array( array( $src, $dst ) ) );
+       }
+
+       /**
+        * Purge a file from the repo. This does no locking nor journaling.
+        * This function can be used to write to otherwise read-only foreign repos.
+        * This is intended for purging thumbnails.
+        *
+        * @param $path string Virtual URL or storage path
+        * @return FileRepoStatus
+        */
+       final public function quickPurge( $path ) {
+               return $this->quickPurgeBatch( array( $path ) );
+       }
+
+       /**
+        * Deletes a directory if empty.
+        * This function can be used to write to otherwise read-only foreign repos.
+        *
+        * @param $dir string Virtual URL (or storage path) of directory to clean
+        * @return Status
+        */
+       public function quickCleanDir( $dir ) {
+               $status = $this->newGood();
+               $status->merge( $this->backend->clean(
+                       array( 'dir' => $this->resolveToStoragePath( $dir ) ) ) );
+
+               return $status;
+       }
+
+       /**
+        * Import a batch of files from the local file system into the repo.
+        * This does no locking nor journaling and overrides existing files.
+        * This function can be used to write to otherwise read-only foreign repos.
+        * This is intended for copying generated thumbnails into the repo.
+        *
+        * @param $pairs Array List of tuples (file system path, virtual URL or storage path)
+        * @return FileRepoStatus
+        */
+       public function quickImportBatch( array $pairs ) {
+               $status = $this->newGood();
+               $operations = array();
+               foreach ( $pairs as $pair ) {
+                       list ( $src, $dst ) = $pair;
+                       $operations[] = array(
+                               'op'        => 'store',
+                               'src'       => $src,
+                               'dst'       => $this->resolveToStoragePath( $dst )
+                       );
+                       $this->backend->prepare( array( 'dir' => dirname( $dst ) ) );
+               }
+               $status->merge( $this->backend->doQuickOperations( $operations ) );
+
+               return $status;
+       }
+
+       /**
+        * Purge a batch of files from the repo.
+        * This function can be used to write to otherwise read-only foreign repos.
+        * This does no locking nor journaling and is intended for purging thumbnails.
+        *
+        * @param $paths Array List of virtual URLs or storage paths
+        * @return FileRepoStatus
         */
-       abstract function storeBatch( $triplets, $flags = 0 );
+       public function quickPurgeBatch( array $paths ) {
+               $status = $this->newGood();
+               $operations = array();
+               foreach ( $paths as $path ) {
+                       $operations[] = array(
+                               'op'                  => 'delete',
+                               'src'                 => $this->resolveToStoragePath( $path ),
+                               'ignoreMissingSource' => true
+                       );
+               }
+               $status->merge( $this->backend->doQuickOperations( $operations ) );
+
+               return $status;
+       }
 
        /**
         * Pick a random name in the temp zone and store a file to it.
-        * Returns a FileRepoStatus object with the URL in the value.
+        * Returns a FileRepoStatus object with the file Virtual URL in the value,
+        * file can later be disposed using FileRepo::freeTemp().
         *
         * @param $originalName String: the base name of the file as specified
         *     by the user. The file extension will be maintained.
         * @param $srcPath String: the current location of the file.
+        * @return FileRepoStatus object with the URL in the value.
         */
-       abstract function storeTemp( $originalName, $srcPath );
+       public function storeTemp( $originalName, $srcPath ) {
+               $this->assertWritableRepo(); // fail out if read-only
+
+               $date      = gmdate( "YmdHis" );
+               $hashPath  = $this->getHashPath( $originalName );
+               $dstRel    = "{$hashPath}{$date}!{$originalName}";
+               $dstUrlRel = $hashPath . $date . '!' . rawurlencode( $originalName );
 
+               $result = $this->store( $srcPath, 'temp', $dstRel, self::SKIP_LOCKING );
+               $result->value = $this->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
+
+               return $result;
+       }
 
        /**
-        * Append the contents of the source path to the given file.
-        * @param $srcPath String: location of the source file
-        * @param $toAppendPath String: path to append to.
-        * @param $flags Integer: bitfield, may be FileRepo::DELETE_SOURCE to indicate
-        *        that the source file should be deleted if possible
-        * @return mixed Status or false
+        * Concatenate a list of files into a target file location.
+        *
+        * @param $srcPaths Array Ordered list of source virtual URLs/storage paths
+        * @param $dstPath String Target file system path
+        * @param $flags Integer: bitwise combination of the following flags:
+        *     self::DELETE_SOURCE     Delete the source files
+        * @return FileRepoStatus
         */
-       abstract function append( $srcPath, $toAppendPath, $flags = 0 );
+       public function concatenate( array $srcPaths, $dstPath, $flags = 0 ) {
+               $this->assertWritableRepo(); // fail out if read-only
+
+               $status = $this->newGood();
+
+               $sources = array();
+               $deleteOperations = array(); // post-concatenate ops
+               foreach ( $srcPaths as $srcPath ) {
+                       // Resolve source to a storage path if virtual
+                       $source = $this->resolveToStoragePath( $srcPath );
+                       $sources[] = $source; // chunk to merge
+                       if ( $flags & self::DELETE_SOURCE ) {
+                               $deleteOperations[] = array( 'op' => 'delete', 'src' => $source );
+                       }
+               }
+
+               // Concatenate the chunks into one FS file
+               $params = array( 'srcs' => $sources, 'dst' => $dstPath );
+               $status->merge( $this->backend->concatenate( $params ) );
+               if ( !$status->isOK() ) {
+                       return $status;
+               }
+
+               // Delete the sources if required
+               if ( $deleteOperations ) {
+                       $opts = array( 'force' => true );
+                       $status->merge( $this->backend->doOperations( $deleteOperations, $opts ) );
+               }
+
+               // Make sure status is OK, despite any $deleteOperations fatals
+               $status->setResult( true );
+
+               return $status;
+       }
 
        /**
         * Remove a temporary file or mark it for garbage collection
-        * @param $virtualUrl String: the virtual URL returned by storeTemp
+        *
+        * @param $virtualUrl String: the virtual URL returned by FileRepo::storeTemp()
         * @return Boolean: true on success, false on failure
-        * STUB
         */
-       function freeTemp( $virtualUrl ) {
-               return true;
+       public function freeTemp( $virtualUrl ) {
+               $this->assertWritableRepo(); // fail out if read-only
+
+               $temp = "mwrepo://{$this->name}/temp";
+               if ( substr( $virtualUrl, 0, strlen( $temp ) ) != $temp ) {
+                       wfDebug( __METHOD__.": Invalid temp virtual URL\n" );
+                       return false;
+               }
+               $path = $this->resolveVirtualUrl( $virtualUrl );
+
+               return $this->cleanupBatch( array( $path ), self::SKIP_LOCKING )->isOK();
        }
 
        /**
-        * Copy or move a file either from the local filesystem or from an mwrepo://
-        * virtual URL, into this repository at the specified destination location.
+        * Copy or move a file either from a storage path, virtual URL,
+        * or FS path, into this repository at the specified destination location.
         *
         * Returns a FileRepoStatus object. On success, the value contains "new" or
         * "archived", to indicate whether the file was new with that name.
         *
-        * @param $srcPath String: the source path or URL
+        * @param $srcPath String: the source FS path, storage path, or URL
         * @param $dstRel String: the destination relative path
-        * @param $archiveRel String: rhe relative path where the existing file is to
+        * @param $archiveRel String: the relative path where the existing file is to
         *        be archived, if there is one. Relative to the public zone root.
         * @param $flags Integer: bitfield, may be FileRepo::DELETE_SOURCE to indicate
         *        that the source file should be deleted if possible
+        * @return FileRepoStatus
         */
-       function publish( $srcPath, $dstRel, $archiveRel, $flags = 0 ) {
+       public function publish( $srcPath, $dstRel, $archiveRel, $flags = 0 ) {
+               $this->assertWritableRepo(); // fail out if read-only
+
                $status = $this->publishBatch( array( array( $srcPath, $dstRel, $archiveRel ) ), $flags );
                if ( $status->successCount == 0 ) {
                        $status->ok = false;
@@ -481,31 +1012,177 @@ abstract class FileRepo {
                } else {
                        $status->value = false;
                }
+
                return $status;
        }
 
        /**
         * Publish a batch of files
-        * @param $triplets Array: (source,dest,archive) triplets as per publish()
+        *
+        * @param $triplets Array: (source, dest, archive) triplets as per publish()
         * @param $flags Integer: bitfield, may be FileRepo::DELETE_SOURCE to indicate
         *        that the source files should be deleted if possible
+        * @throws MWException
+        * @return FileRepoStatus
         */
-       abstract function publishBatch( $triplets, $flags = 0 );
+       public function publishBatch( array $triplets, $flags = 0 ) {
+               $this->assertWritableRepo(); // fail out if read-only
+
+               $backend = $this->backend; // convenience
+               // Try creating directories
+               $status = $this->initZones( 'public' );
+               if ( !$status->isOK() ) {
+                       return $status;
+               }
 
-       function fileExists( $file, $flags = 0 ) {
-               $result = $this->fileExistsBatch( array( $file ), $flags );
+               $status = $this->newGood( array() );
+
+               $operations = array();
+               $sourceFSFilesToDelete = array(); // cleanup for disk source files
+               // Validate each triplet and get the store operation...
+               foreach ( $triplets as $i => $triplet ) {
+                       list( $srcPath, $dstRel, $archiveRel ) = $triplet;
+                       // Resolve source to a storage path if virtual
+                       $srcPath = $this->resolveToStoragePath( $srcPath );
+                       if ( !$this->validateFilename( $dstRel ) ) {
+                               throw new MWException( 'Validation error in $dstRel' );
+                       }
+                       if ( !$this->validateFilename( $archiveRel ) ) {
+                               throw new MWException( 'Validation error in $archiveRel' );
+                       }
+
+                       $publicRoot = $this->getZonePath( 'public' );
+                       $dstPath = "$publicRoot/$dstRel";
+                       $archivePath = "$publicRoot/$archiveRel";
+
+                       $dstDir = dirname( $dstPath );
+                       $archiveDir = dirname( $archivePath );
+                       // Abort immediately on directory creation errors since they're likely to be repetitive
+                       if ( !$backend->prepare( array( 'dir' => $dstDir ) )->isOK() ) {
+                               return $this->newFatal( 'directorycreateerror', $dstDir );
+                       }
+                       if ( !$backend->prepare( array( 'dir' => $archiveDir ) )->isOK() ) {
+                               return $this->newFatal( 'directorycreateerror', $archiveDir );
+                       }
+
+                       // Archive destination file if it exists
+                       if ( $backend->fileExists( array( 'src' => $dstPath ) ) ) {
+                               // Check if the archive file exists
+                               // This is a sanity check to avoid data loss. In UNIX, the rename primitive
+                               // unlinks the destination file if it exists. DB-based synchronisation in
+                               // publishBatch's caller should prevent races. In Windows there's no
+                               // problem because the rename primitive fails if the destination exists.
+                               if ( $backend->fileExists( array( 'src' => $archivePath ) ) ) {
+                                       $operations[] = array( 'op' => 'null' );
+                                       continue;
+                               } else {
+                                       $operations[] = array(
+                                               'op'           => 'move',
+                                               'src'          => $dstPath,
+                                               'dst'          => $archivePath
+                                       );
+                               }
+                               $status->value[$i] = 'archived';
+                       } else {
+                               $status->value[$i] = 'new';
+                       }
+                       // Copy (or move) the source file to the destination
+                       if ( FileBackend::isStoragePath( $srcPath ) ) {
+                               if ( $flags & self::DELETE_SOURCE ) {
+                                       $operations[] = array(
+                                               'op'           => 'move',
+                                               'src'          => $srcPath,
+                                               'dst'          => $dstPath
+                                       );
+                               } else {
+                                       $operations[] = array(
+                                               'op'           => 'copy',
+                                               'src'          => $srcPath,
+                                               'dst'          => $dstPath
+                                       );
+                               }
+                       } else { // FS source path
+                               $operations[] = array(
+                                       'op'           => 'store',
+                                       'src'          => $srcPath,
+                                       'dst'          => $dstPath
+                               );
+                               if ( $flags & self::DELETE_SOURCE ) {
+                                       $sourceFSFilesToDelete[] = $srcPath;
+                               }
+                       }
+               }
+
+               // Execute the operations for each triplet
+               $opts = array( 'force' => true );
+               $status->merge( $backend->doOperations( $operations, $opts ) );
+               // Cleanup for disk source files...
+               foreach ( $sourceFSFilesToDelete as $file ) {
+                       wfSuppressWarnings();
+                       unlink( $file ); // FS cleanup
+                       wfRestoreWarnings();
+               }
+
+               return $status;
+       }
+
+       /**
+        * Deletes a directory if empty.
+        *
+        * @param $dir string Virtual URL (or storage path) of directory to clean
+        * @return Status
+        */
+       public function cleanDir( $dir ) {
+               $this->assertWritableRepo(); // fail out if read-only
+
+               $status = $this->newGood();
+               $status->merge( $this->backend->clean(
+                       array( 'dir' => $this->resolveToStoragePath( $dir ) ) ) );
+
+               return $status;
+       }
+
+       /**
+        * Checks existence of a a file
+        *
+        * @param $file string Virtual URL (or storage path) of file to check
+        * @return bool
+        */
+       public function fileExists( $file ) {
+               $result = $this->fileExistsBatch( array( $file ) );
                return $result[0];
        }
 
        /**
         * Checks existence of an array of files.
         *
-        * @param $files Array: URLs (or paths) of files to check
-        * @param $flags Integer: bitwise combination of the following flags:
-        *     self::FILES_ONLY     Mark file as existing only if it is a file (not directory)
-        * @return Either array of files and existence flags, or false
+        * @param $files Array: Virtual URLs (or storage paths) of files to check
+        * @return array|bool Either array of files and existence flags, or false
         */
-       abstract function fileExistsBatch( $files, $flags = 0 );
+       public function fileExistsBatch( array $files ) {
+               $result = array();
+               foreach ( $files as $key => $file ) {
+                       $file = $this->resolveToStoragePath( $file );
+                       $result[$key] = $this->backend->fileExists( array( 'src' => $file ) );
+               }
+               return $result;
+       }
+
+       /**
+        * Move a file to the deletion archive.
+        * If no valid deletion archive exists, this may either delete the file
+        * or throw an exception, depending on the preference of the repository
+        *
+        * @param $srcRel Mixed: relative path for the file to be deleted
+        * @param $archiveRel Mixed: relative path for the archive location.
+        *        Relative to a private archive directory.
+        * @return FileRepoStatus object
+        */
+       public function delete( $srcRel, $archiveRel ) {
+               $this->assertWritableRepo(); // fail out if read-only
+
+               return $this->deleteBatch( array( array( $srcRel, $archiveRel ) ) );
+       }
 
        /**
         * Move a group of files to the deletion archive.
@@ -513,7 +1190,7 @@ abstract class FileRepo {
         * If no valid deletion archive is configured, this may either delete the
         * file or throw an exception, depending on the preference of the repository.
         *
-        * The overwrite policy is determined by the repository -- currently FSRepo
+        * The overwrite policy is determined by the repository -- currently LocalRepo
         * assumes a naming scheme in the deleted zone based on content hash, as
         * opposed to the public zone which is assumed to be unique.
         *
@@ -521,77 +1198,238 @@ abstract class FileRepo {
         *        is a two-element array containing the source file path relative to the
         *        public root in the first element, and the archive file path relative
         *        to the deleted zone root in the second element.
+        * @throws MWException
         * @return FileRepoStatus
         */
-       abstract function deleteBatch( $sourceDestPairs );
+       public function deleteBatch( array $sourceDestPairs ) {
+               $this->assertWritableRepo(); // fail out if read-only
+
+               // Try creating directories
+               $status = $this->initZones( array( 'public', 'deleted' ) );
+               if ( !$status->isOK() ) {
+                       return $status;
+               }
+
+               $status = $this->newGood();
+
+               $backend = $this->backend; // convenience
+               $operations = array();
+               // Validate filenames and create archive directories
+               foreach ( $sourceDestPairs as $pair ) {
+                       list( $srcRel, $archiveRel ) = $pair;
+                       if ( !$this->validateFilename( $srcRel ) ) {
+                               throw new MWException( __METHOD__.':Validation error in $srcRel' );
+                       } elseif ( !$this->validateFilename( $archiveRel ) ) {
+                               throw new MWException( __METHOD__.':Validation error in $archiveRel' );
+                       }
+
+                       $publicRoot = $this->getZonePath( 'public' );
+                       $srcPath = "{$publicRoot}/$srcRel";
+
+                       $deletedRoot = $this->getZonePath( 'deleted' );
+                       $archivePath = "{$deletedRoot}/{$archiveRel}";
+                       $archiveDir = dirname( $archivePath ); // does not touch FS
+
+                       // Create destination directories
+                       if ( !$backend->prepare( array( 'dir' => $archiveDir ) )->isOK() ) {
+                               return $this->newFatal( 'directorycreateerror', $archiveDir );
+                       }
+                       $this->initDeletedDir( $archiveDir );
+
+                       $operations[] = array(
+                               'op'            => 'move',
+                               'src'           => $srcPath,
+                               'dst'           => $archivePath,
+                               // We may have 2+ identical files being deleted,
+                               // all of which will map to the same destination file
+                               'overwriteSame' => true // also see bug 31792
+                       );
+               }
+
+               // Move the files by execute the operations for each pair.
+               // We're now committed to returning an OK result, which will
+               // lead to the files being moved in the DB also.
+               $opts = array( 'force' => true );
+               $status->merge( $backend->doOperations( $operations, $opts ) );
+
+               return $status;
+       }
+
+       /**
+        * Delete files in the deleted directory if they are not referenced in the filearchive table
+        *
+        * STUB
+        */
+       public function cleanupDeletedBatch( array $storageKeys ) {
+               $this->assertWritableRepo();
+       }
 
        /**
-        * Move a file to the deletion archive.
-        * If no valid deletion archive exists, this may either delete the file
-        * or throw an exception, depending on the preference of the repository
-        * @param $srcRel Mixed: relative path for the file to be deleted
-        * @param $archiveRel Mixed: relative path for the archive location.
-        *        Relative to a private archive directory.
-        * @return FileRepoStatus object
+        * Get a relative path for a deletion archive key,
+        * e.g. s/z/a/ for sza251lrxrc1jad41h5mgilp8nysje52.jpg
+        *
+        * @param $key string
+        * @return string
         */
-       function delete( $srcRel, $archiveRel ) {
-               return $this->deleteBatch( array( array( $srcRel, $archiveRel ) ) );
+       public function getDeletedHashPath( $key ) {
+               $path = '';
+               for ( $i = 0; $i < $this->deletedHashLevels; $i++ ) {
+                       $path .= $key[$i] . '/';
+               }
+               return $path;
        }
 
        /**
-        * Get properties of a file with a given virtual URL
-        * The virtual URL must refer to this repo
-        * Properties should ultimately be obtained via File::getPropsFromPath()
+        * If a path is a virtual URL, resolve it to a storage path.
+        * Otherwise, just return the path as it is.
+        *
+        * @param $path string
+        * @return string
+        * @throws MWException
         */
-       abstract function getFileProps( $virtualUrl );
+       protected function resolveToStoragePath( $path ) {
+               if ( $this->isVirtualUrl( $path ) ) {
+                       return $this->resolveVirtualUrl( $path );
+               }
+               return $path;
+       }
 
        /**
-        * Call a callback function for every file in the repository
-        * May use either the database or the filesystem
-        * STUB
+        * Get a local FS copy of a file with a given virtual URL/storage path.
+        * Temporary files may be purged when the file object falls out of scope.
+        *
+        * @param $virtualUrl string
+        * @return TempFSFile|null Returns null on failure
         */
-       function enumFiles( $callback ) {
-               throw new MWException( 'enumFiles is not supported by ' . get_class( $this ) );
+       public function getLocalCopy( $virtualUrl ) {
+               $path = $this->resolveToStoragePath( $virtualUrl );
+               return $this->backend->getLocalCopy( array( 'src' => $path ) );
        }
 
        /**
-        * Determine if a relative path is valid, i.e. not blank or involving directory traveral
+        * Get a local FS file with a given virtual URL/storage path.
+        * The file is either an original or a copy. It should not be changed.
+        * Temporary files may be purged when the file object falls out of scope.
+        *
+        * @param $virtualUrl string
+        * @return FSFile|null Returns null on failure.
         */
-       function validateFilename( $filename ) {
-               if ( strval( $filename ) == '' ) {
+       public function getLocalReference( $virtualUrl ) {
+               $path = $this->resolveToStoragePath( $virtualUrl );
+               return $this->backend->getLocalReference( array( 'src' => $path ) );
+       }
+
+       /**
+        * Get properties of a file with a given virtual URL/storage path.
+        * Properties should ultimately be obtained via FSFile::getProps().
+        *
+        * @param $virtualUrl string
+        * @return Array
+        */
+       public function getFileProps( $virtualUrl ) {
+               $path = $this->resolveToStoragePath( $virtualUrl );
+               return $this->backend->getFileProps( array( 'src' => $path ) );
+       }
+
+       /**
+        * Get the timestamp of a file with a given virtual URL/storage path
+        *
+        * @param $virtualUrl string
+        * @return string|bool False on failure
+        */
+       public function getFileTimestamp( $virtualUrl ) {
+               $path = $this->resolveToStoragePath( $virtualUrl );
+               return $this->backend->getFileTimestamp( array( 'src' => $path ) );
+       }
+
+       /**
+        * Get the sha1 of a file with a given virtual URL/storage path
+        *
+        * @param $virtualUrl string
+        * @return string|bool
+        */
+       public function getFileSha1( $virtualUrl ) {
+               $path = $this->resolveToStoragePath( $virtualUrl );
+               $tmpFile = $this->backend->getLocalReference( array( 'src' => $path ) );
+               if ( !$tmpFile ) {
                        return false;
                }
-               if ( wfIsWindows() ) {
-                       $filename = strtr( $filename, '\\', '/' );
-               }
-               /**
-                * Use the same traversal protection as Title::secureAndSplit()
-                */
-               if ( strpos( $filename, '.' ) !== false &&
-                    ( $filename === '.' || $filename === '..' ||
-                      strpos( $filename, './' ) === 0  ||
-                      strpos( $filename, '../' ) === 0 ||
-                      strpos( $filename, '/./' ) !== false ||
-                      strpos( $filename, '/../' ) !== false ) )
-               {
-                       return false;
-               } else {
-                       return true;
+               return $tmpFile->getSha1Base36();
+       }
+
+       /**
+        * Attempt to stream a file with the given virtual URL/storage path
+        *
+        * @param $virtualUrl string
+        * @param $headers Array Additional HTTP headers to send on success
+        * @return bool Success
+        */
+       public function streamFile( $virtualUrl, $headers = array() ) {
+               $path = $this->resolveToStoragePath( $virtualUrl );
+               $params = array( 'src' => $path, 'headers' => $headers );
+               return $this->backend->streamFile( $params )->isOK();
+       }
+
+       /**
+        * Call a callback function for every public regular file in the repository.
+        * This only acts on the current version of files, not any old versions.
+        * May use either the database or the filesystem.
+        *
+        * @param $callback Array|string
+        * @return void
+        */
+       public function enumFiles( $callback ) {
+               $this->enumFilesInStorage( $callback );
+       }
+
+       /**
+        * Call a callback function for every public file in the repository.
+        * May use either the database or the filesystem.
+        *
+        * @param $callback Array|string
+        * @return void
+        */
+       protected function enumFilesInStorage( $callback ) {
+               $publicRoot = $this->getZonePath( 'public' );
+               $numDirs = 1 << ( $this->hashLevels * 4 );
+               // Use a priori assumptions about directory structure
+               // to reduce the tree height of the scanning process.
+               for ( $flatIndex = 0; $flatIndex < $numDirs; $flatIndex++ ) {
+                       $hexString = sprintf( "%0{$this->hashLevels}x", $flatIndex );
+                       $path = $publicRoot;
+                       for ( $hexPos = 0; $hexPos < $this->hashLevels; $hexPos++ ) {
+                               $path .= '/' . substr( $hexString, 0, $hexPos + 1 );
+                       }
+                       $iterator = $this->backend->getFileList( array( 'dir' => $path ) );
+                       foreach ( $iterator as $name ) {
+                               // Each item returned is a public file
+                               call_user_func( $callback, "{$path}/{$name}" );
+                       }
                }
        }
 
-       /**#@+
-        * Path disclosure protection functions
+       /**
+        * Determine if a relative path is valid, i.e. not blank or involving directory traveral
+        *
+        * @param $filename string
+        * @return bool
         */
-       function paranoidClean( $param ) { return '[hidden]'; }
-       function passThrough( $param ) { return $param; }
+       public function validateFilename( $filename ) {
+               if ( strval( $filename ) == '' ) {
+                       return false;
+               }
+               return FileBackend::isPathTraversalFree( $filename );
+       }
 
        /**
         * Get a callback function to use for cleaning error message parameters
+        *
+        * @return Array
         */
        function getErrorCleanupFunction() {
                switch ( $this->pathDisclosureProtection ) {
                        case 'none':
+                       case 'simple': // b/c
                                $callback = array( $this, 'passThrough' );
                                break;
                        default: // 'paranoid'
@@ -599,30 +1437,48 @@ abstract class FileRepo {
                }
                return $callback;
        }
-       /**#@-*/
+
+       /**
+        * Path disclosure protection function
+        *
+        * @param $param string
+        * @return string
+        */
+       function paranoidClean( $param ) {
+               return '[hidden]';
+       }
+
+       /**
+        * Path disclosure protection function
+        *
+        * @param $param string
+        * @return string
+        */
+       function passThrough( $param ) {
+               return $param;
+       }
 
        /**
         * Create a new fatal error
+        *
+        * @return FileRepoStatus
         */
-       function newFatal( $message /*, parameters...*/ ) {
+       public function newFatal( $message /*, parameters...*/ ) {
                $params = func_get_args();
                array_unshift( $params, $this );
-               return call_user_func_array( array( 'FileRepoStatus', 'newFatal' ), $params );
+               return MWInit::callStaticMethod( 'FileRepoStatus', 'newFatal', $params );
        }
 
        /**
         * Create a new good result
+        *
+        * @param $value null|string
+        * @return FileRepoStatus
         */
-       function newGood( $value = null ) {
+       public function newGood( $value = null ) {
                return FileRepoStatus::newGood( $this, $value );
        }
 
-       /**
-        * Delete files in the deleted directory if they are not referenced in the filearchive table
-        * STUB
-        */
-       function cleanupDeletedBatch( $storageKeys ) {}
-
        /**
         * Checks if there is a redirect named as $title. If there is, return the
         * title object. If not, return false.
@@ -631,7 +1487,7 @@ abstract class FileRepo {
         * @param $title Title of image
         * @return Bool
         */
-       function checkRedirect( $title ) {
+       public function checkRedirect( Title $title ) {
                return false;
        }
 
@@ -642,20 +1498,11 @@ abstract class FileRepo {
         * STUB
         * @param $title Title of image
         */
-       function invalidateImageRedirect( $title ) {}
+       public function invalidateImageRedirect( Title $title ) {}
 
        /**
-        * Get an array or iterator of file objects for files that have a given
-        * SHA-1 content hash.
+        * Get the human-readable name of the repo
         *
-        * STUB
-        */
-       function findBySha1( $hash ) {
-               return array();
-       }
-
-       /**
-        * Get the human-readable name of the repo.
         * @return string
         */
        public function getDisplayName() {
@@ -672,19 +1519,19 @@ abstract class FileRepo {
         *
         * @return bool
         */
-       function isLocal() {
+       public function isLocal() {
                return $this->getName() == 'local';
        }
 
-
        /**
         * Get a key on the primary cache for this repository.
         * Returns false if the repository's cache is not accessible at this site.
         * The parameters are the parts of the key, as for wfMemcKey().
         *
         * STUB
+        * @return bool
         */
-       function getSharedCacheKey( /*...*/ ) {
+       public function getSharedCacheKey( /*...*/ ) {
                return false;
        }
 
@@ -692,17 +1539,69 @@ abstract class FileRepo {
         * Get a key for this repo in the local cache domain. These cache keys are
         * not shared with remote instances of the repo.
         * The parameters are the parts of the key, as for wfMemcKey().
+        *
+        * @return string
         */
-       function getLocalCacheKey( /*...*/ ) {
+       public function getLocalCacheKey( /*...*/ ) {
                $args = func_get_args();
                array_unshift( $args, 'filerepo', $this->getName() );
                return call_user_func_array( 'wfMemcKey', $args );
        }
-       
+
+       /**
+        * Get an temporary FileRepo associated with this repo.
+        * Files will be created in the temp zone of this repo and
+        * thumbnails in a /temp subdirectory in thumb zone of this repo.
+        * It will have the same backend as this repo.
+        *
+        * @return TempFileRepo
+        */
+       public function getTempRepo() {
+               return new TempFileRepo( array(
+                       'name'      => "{$this->name}-temp",
+                       'backend'   => $this->backend,
+                       'zones'     => array(
+                               'public' => array(
+                                       'container' => $this->zones['temp']['container'],
+                                       'directory' => $this->zones['temp']['directory']
+                               ),
+                               'thumb'  => array(
+                                       'container' => $this->zones['thumb']['container'],
+                                       'directory' => ( $this->zones['thumb']['directory'] == '' )
+                                               ? 'temp'
+                                               : $this->zones['thumb']['directory'] . '/temp'
+                               )
+                       ),
+                       'url'        => $this->getZoneUrl( 'temp' ),
+                       'thumbUrl'   => $this->getZoneUrl( 'thumb' ) . '/temp',
+                       'hashLevels' => $this->hashLevels // performance
+               ) );
+       }
+
        /**
         * Get an UploadStash associated with this repo.
+        *
+        * @return UploadStash
         */
-       function getUploadStash() {
+       public function getUploadStash() {
                return new UploadStash( $this );
        }
+
+       /**
+        * Throw an exception if this repo is read-only by design.
+        * This does not and should not check getReadOnlyReason().
+        *
+        * @return void
+        * @throws MWException
+        */
+       protected function assertWritableRepo() {}
+}
+
+/**
+ * FileRepo for temporary files created via FileRepo::getTempRepo()
+ */
+class TempFileRepo extends FileRepo {
+       public function getTempRepo() {
+               throw new MWException( "Cannot get a temp repo from a temp repo." );
+       }
 }